find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIRS})
add_definitions(${PNG_DEFINITIONS})
add_definitions(-DGLUI_NO_LIB_PRAGMA)

set(SHOW_LIBS scan ${SPNAV_LIB} ${PNG_LIBRARIES} ${ANN_LIBRARIES} ${NEWMAT_LIBRARIES} ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_PROGRAM_OPTIONS_LIBRARY} CACHE INTERNAL "")

if (WITH_OPENGL)
  find_package(OpenGL REQUIRED)
  find_package(GLUT REQUIRED)
  add_definitions(-DWITH_OPENGL)
  if(APPLE)
    include_directories(/usr/local/Cellar/glui/2.37/)
    set(SHOW_LIBS ${SHOW_LIBS} ${GLUT_LIBRARIES} ${OPENGL_LIBRARIES} /usr/local/Cellar/glui/2.37/lib/libglui.a CACHE INTERNAL "")
  else()
    include_directories(${PROJECT_SOURCE_DIR}/3rdparty/glui-2.36/include/)
    set(SHOW_LIBS ${SHOW_LIBS} ${GLUT_LIBRARIES} ${OPENGL_LIBRARIES} glui CACHE INTERNAL "")
  endif()
  if(WITH_GLEE)
    message(STATUS "Using opengl extensions")
    set(SHOW_LIBS ${SHOW_LIBS} glee CACHE INTERNAL "")
    include_directories(${PROJECT_SOURCE_DIR}/3rdparty/glee)
    add_definitions(-DWITH_GLEE)
  else()
    message(STATUS "Not using opengl extensions")
  endif()
endif()

add_library(show_objects OBJECT NurbsPath.cc PathGraph.cc scancolormanager.cc colormanager.cc compacttree.cc show_gl.cc vertexarray.cc viewcull.cc display.cc show_animate.cc show_common.cc show_menu.cc program_options.cc callbacks_glut.cpp cylinderFitting.cc)
set_property(TARGET show_objects PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(show show.cc $<TARGET_OBJECTS:show_objects>)
target_link_libraries(show ${SHOW_LIBS})

# FIXME: is opengl necessary for everything including the libs?
if (WITH_OPENGL)
  include_directories(${GLUT_INCLUDE_DIR})

  if(WITH_COMPACT_OCTREE)
    message(STATUS "Using compact octrees")
    add_definitions(-DUSE_COMPACT_TREE)
  else()
    message(STATUS "Not using compact octreees: Warning uses more memory")
  endif()

  if(WITH_IMGUI)
    include_directories(${IMGUI_INCLUDE_DIRS})
    add_executable(imshowbin imshow.cc $<TARGET_OBJECTS:show_objects> ${IMGUI_SRCS})
    set_target_properties(imshowbin PROPERTIES OUTPUT_NAME "imshow")
    target_link_libraries(imshowbin ${SHOW_LIBS})
  endif()

  add_executable(showbin show.cc $<TARGET_OBJECTS:show_objects>)
  set_target_properties(showbin PROPERTIES OUTPUT_NAME "show")
  target_link_libraries(showbin ${SHOW_LIBS})
  if(WITH_3DMOUSE)
    target_compile_definitions(showbin PRIVATE SPACEMOUSE)
    if (WITH_IMGUI)
      target_compile_definitions(imshowbin PRIVATE SPACEMOUSE)
    endif()
  endif()
  if(WITH_WXWIDGETS)
    find_package(wxWidgets REQUIRED core base gl)
    # set wxWidgets_wxrc_EXECUTABLE to be ignored in the configuration
    set (wxWidgets_wxrc_EXECUTABLE " " CACHE INTERNAL "" FORCE)
    # wxWidgets include (this will do all the magic to configure everything)
    include( ${wxWidgets_USE_FILE})
    add_executable(wxshow wxshow.cc selectionframe.cc $<TARGET_OBJECTS:show_objects>)
    target_link_libraries(wxshow ${wxWidgets_LIBRARIES} wxthings ${SHOW_LIBS})
  else()
    message(STATUS "Not building wxshow because WITH_WXWIDGETS is OFF")
  endif()
else()
  message(STATUS "Not building show because WITH_OPENGL is OFF")
endif()
